/*
 * @Description: 
 * @Author: yyh
 * @Date: 2022-03-28 21:54:35
 * @LastEditors: yyh
 * @LastEditTime: 2022-03-29 00:50:28
 */
var db = require("../utils/db");

//新增（创建）资产分类
addClassification = async (req, res) => {
    var { name } = req.body;
    var sql = "insert into classification (name) values(?)";
    var sqlArr = [name];
    var result = await db.SySqlConnect(sql, sqlArr);
    if (result.affectedRows == 1) {
        res.send({
            code: 200,
            msg: "新增成功",
        });
    } else {
        res.send({
            code: 400,
            msg: "新增失败",
        });
    }
}

//删除资产分类
deleteClassification = async (req, res) => {
    var id = req.query[0];
    var sql = "delete from classification where id = ? ";
    var sqlArr = [id];
    var result = await db.SySqlConnect(sql, sqlArr);
    if (result.affectedRows == 1) {
        res.send({
            code: 200,
            msg: "删除成功",
        });
    } else {
        res.send({
            code: 400,
            msg: "删除失败",
        });
    }
};

//模糊查询资产分类
getClassification = async (req, res) => {
    var { name } = req.query;
    var sql = "select * from classification";
    var sqlArr = [];
    if (name) {
        sql += " where name like ?";
        sqlArr.push("%" + name + "%");
      }
      var callBack = async (err, data) => {
        if (err) {
          console.log(err);
          res.send({
            code: 400,
            msg: "模糊查询失败",
            data: {},
          });
        } else {
          res.send({
            code: 200,
            msg: "模糊查询成功",
            data: data,
          });
        }
      };
      db.sqlConnect(sql, sqlArr, callBack);
}

module.exports = {
    addClassification,
    deleteClassification,
    getClassification
};